export default function (el) {
	let startX;
	let startTime;
	el.addEventListener('touchstart', function (e) {
		startX = e.touches[0].pageX;
		startTime = e.timeStamp;
	});

	el.addEventListener('touchmove', function (e) {
		let moveX = e.changedTouches[0].pageX;
		if (startX - moveX < 0) {
			el.style.transition = 'none';
			el.style.transform = `translate3d(${-(startX - moveX)}px, 0, 0)`;
		}
	});
	el.addEventListener('touchend', function (e) {
		let endX = e.changedTouches[0].pageX;
		let endTime = e.timeStamp;
		let moveStr = el.style.transform;
		let moveStrSlice = moveStr.slice(moveStr.search(/\(/g)).split(',');
		let moveX = moveStrSlice[0].slice(1, -2);
		let elWidth = el.offsetWidth;
		if (Math.abs(endX - startX) > 15) {
			el.style.transition = 'transform .6s';
			if (endTime - startTime < 80 && endX - startX > 0) {
				el.style.transform = `translate3d(${elWidth}px, 0, 0)`;
				window.history.go(-1);
				return;
			}
			if (moveX > elWidth / 2) {
				el.style.transform = `translate3d(${elWidth}px, 0, 0)`;
				window.history.go(-1);
			} else {
				el.style.transform = `translate3d(${0}px, 0, 0)`
			}
		}
	});
	el.addEventListener('transitionEnd', function () {
		el.style.transition = 'none'
	})
}
